package com.max.eshop.storage;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

import com.max.eshop.comporators.*;
import com.max.eshop.core.Book;
import com.max.eshop.core.Order;
import com.max.eshop.core.Reqest;
import com.max.eshop.parsers.*;
import com.max.eshop.services.Files;

public class Storage {
	public List<Book> books;
	private List<Order> orders;
	private List<Reqest> reqests;
	private List<Order> executeOrders = new ArrayList<Order>();
	private List<Book> booksStale = new ArrayList<Book>();
	ParserBooks pb = new ParserBooks();
	ParserOrder po = new ParserOrder();
	ParserReqest pr = new ParserReqest();
	Files files;

	public void readFromFiles(String path) {
		files = new Files(path);
		books = pb.returnBooks(files.readBooksFromFiles());
		orders = po.returnOrders(files.readOrdersFromFiles());
		reqests = pr.returnReqests(files.readReqestsFromFiles());
	}

	public void writeToFiles() {
		files.writeToFiles(pb.booksToSrting(books), po.booksToString(orders),
				pr.reqestsToSrting(reqests));
	}

	public void showBooks() {

		for (int i = 1; i <= pb.booksToSrting(books).length - 1; i++) {
			System.out.println(pb.booksToSrting(books)[i]);
		}
	}

	public void showOrders() {
		for (int i = 1; i <= po.booksToString(orders).length - 1; i++) {
			System.out.println(po.booksToString(orders)[i]);
		}
	}

	public void showReqest() {
		for (int i = 1; i <= pr.reqestsToSrting(reqests).length - 1; i++) {
			System.out.println(pr.reqestsToSrting(reqests)[i]);
		}
	}

	public void addBook(String name, Date dateOfPublication,
			String description, Integer count, Integer price) {
		Date date = Calendar.getInstance(TimeZone.getTimeZone("GMT+03:00"))
				.getTime();
		int chek = 0;
		for (int i = 0; i <= books.size() - 1; i++) {
			if (books.get(i).getName().equals(name)) {
				books.get(i).setCount(books.get(i).getCount()+count);
				books.get(i).setDateOfPublication(dateOfPublication);
				books.get(i).setPrice(price);
				books.get(i).setDateOfReceipt(date);
				chek = 1;
			}
		}
		if (chek == 0) {
		
			books.add(new Book(name, dateOfPublication, description,
					date, count, price));
		}
	}

	public void addOrder(String name, Integer count) {
		Date date = Calendar.getInstance(TimeZone.getTimeZone("GMT+03:00"))
				.getTime();
		Integer priceOrder = 0;
		int chek = 0;
		for (int i = 0; i <= books.size() - 1; i++) {
			if (books.get(i).getName().equals(name)) {
				priceOrder = books.get(i).getPrice() * count;
				orders.add(new Order(date, false, priceOrder, name, count));
				chek = 1;
			}

		}
		if (chek == 0) {
			int temp = 0;
			for (int j = 0; j <= reqests.size() - 1; j++) {
				if (reqests.get(j).getNameBook().equals(name)) {
					reqests.get(j).setCountReqest(
							reqests.get(j).getCountReqest() + 1);
					temp = 1;
				}
			}
			if (temp == 0) {
				this.addReqest(name, 1);
			}
		}
	}

	public void addReqest(String name, Integer count) {
		int temp = 0;
		for (int j = 0; j <= reqests.size() - 1; j++) {
			if (reqests.get(j).getNameBook().equals(name)) {
				reqests.get(j).setCountReqest(
						reqests.get(j).getCountReqest() + count);
				temp = 1;
			}
		}
		if (temp == 0) {
		reqests.add(new Reqest(name, count));
		}
	}

	public void deleteBook(int i) {
		books.remove(i);
	}

	public void deleteOrder(int i) {
		orders.remove(i);
	}

	public void executeOrder(int i) {
		int temp=0;
		if (orders.get(i).getStatus().equals(false)) {
			for (int j = 0; j <= books.size() - 1; j++) {
				if (books.get(j).getName().equals(orders.get(i).getNameBook())) {
					temp=1;
					if (books.get(j).getCount() >= orders.get(i).getCount()) {
						orders.get(i).setStatus(true);
						books.get(j).setCount(
								books.get(j).getCount()
										- orders.get(i).getCount());
						if (books.get(j).getCount() == 0) {
							this.deleteBook(j);
						}
					}
					else{
						System.out.println("stolbko Knigi nety na sklade.Dobavlena Zayavka");
						reqests.add(new Reqest(orders.get(i).getNameBook(), orders.get(i).getCount()));
					}
				}
				
			
			}
			if(temp==0){
				System.out.println("Takoi Knigi nety na sklade.Dobavlena Zayavka");
				reqests.add(new Reqest(orders.get(i).getNameBook(), 1));			
			}
		} else {
			System.out.println("Zakaz uwe vipolnen");
		}
	}

	public void showCountExecuteOrder(Date ot, Date before) {
		int count = 0;

		for (int i = 0; i <= orders.size() - 1; i++) {
			if (orders.get(i).getDate().after(ot)
					&& orders.get(i).getDate().before(before)
					&& orders.get(i).getStatus().equals(true)) {
				count++;
			}
		}
		System.out.println(count);
	}

	public void showExecuteOrder(Date ot, Date before) {

		for (int i = 0; i <= orders.size() - 1; i++) {
			if (orders.get(i).getDate().after(ot)
					&& orders.get(i).getDate().before(before)
					&& orders.get(i).getStatus().equals(true)) {
				executeOrders.add(orders.get(i));
			}
		}

		for (int i = 1; i <= po.booksToString(executeOrders).length - 1; i++) {
			System.out.println(po.booksToString(executeOrders)[i]);
		}
	}

	public void showSumExecuteOrder(Date ot, Date before) {
		int sumPrice = 0;
		for (int i = 0; i <= orders.size() - 1; i++) {
			if (orders.get(i).getDate().after(ot)
					&& orders.get(i).getDate().before(before)
					&& orders.get(i).getStatus().equals(true)) {
				sumPrice += orders.get(i).getPrice();
			}
		}
		System.out.println(sumPrice);
	}

	public void showStaleBooks() {
		Date dateOt = Calendar.getInstance(TimeZone.getTimeZone("GMT+00:00"))
				.getTime();
		dateOt.setMonth(dateOt.getMonth() - 6);
		for (int i = 0; i <= books.size() - 1; i++) {
			if (books.get(i).getDateOfReceipt().before(dateOt)) {
				booksStale.add(books.get(i));
			}

		}

		for (int i = 1; i <= pb.booksToSrting(booksStale).length - 1; i++) {
			System.out.println(pb.booksToSrting(booksStale)[i]);
		}

	}

	public void sortedReqestByCount() {
		Collections.sort(reqests, new SortedReqestsByCount());
	}

	public void sortedReqestByAlphabet() {
		Collections.sort(reqests, new SortedReqestsByAlphabet());
	}

	public void sortedOrdersByStatus() {
		Collections.sort(orders, new SortedOrdersByStatus());
	}

	public void sortedOrdersByPrice() {
		Collections.sort(orders, new SortedOrdersByPrice());

	}

	public void sortedOrdersByDate() {

		Collections.sort(orders, new SortedOrdersByDate());
	}

	public void sortedExecuteOrdersByPrice() {

		Collections.sort(executeOrders, new SortedOrdersByPrice());

	}

	public void sortedExecuteOrdersByDate() {

		Collections.sort(executeOrders, new SortedOrdersByDate());
	}

	public void sortedBooksByCount() {

		Collections.sort(books, new SortedBooksByCount());
	}

	public void sortedBooksByPrice() {
		Collections.sort(books, new SortedBooksByPrice());

	}

	public void sortedBooksByDateOfPublication() {

		Collections.sort(books, new SortedBooksByDateOfPublication());

	}

	public void sortedBooksByAlphabet() {
		Collections.sort(books, new SortedBooksByAlphabet());

	}

	public void sortedStaleBooksByPrice() {
		Collections.sort(booksStale, new SortedBooksByPrice());

	}

	public void sortedStaleBooksByDateOfReceipt() {
		Collections.sort(booksStale, new SortedBooksByDateOfReceipt());

	}

	public Integer countOrders() {
		return orders.size();
		
	}

	public Integer countBooks() {
		return books.size();
	}

}